<?php
namespace App\Model;
use App\Repository\Sys\Db;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
class StorefrontTable{
    use Db;
    static private $table = 'storefront';

    static function getStoreName($store_id){

       return  StorefrontTable::ORM()->where('id',$store_id)->first()->store_name;
    }

    static function getList($page,$limit,$key){
        $param = [];
        $where = '1=1';
        if ($key) {
            $where .= ' and store_name like :store_name';
            $param['store_name'] = "%{$key}%";
        }

        $count = \DB::select("select count(*) as c  from storefront WHERE {$where}", $param)[0]->c;
        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;
        $res = \DB::select("select * from storefront WHERE {$where} ORDER BY id DESC limit :offset,:limit", $param);

        $data = [
            'count' => $count,
            'list' => $res,
            'page' => $page,

        ];
        return JsonRespose::jsonArray(1,$data);
    }

    static public function updateCountField($store_id)
    {
        $count = FieldTable::ORM()->where('storefront_id', $store_id)->count();
        StorefrontTable::ORM()->where('id', $store_id)->update(['field_count'=>$count]);
    }
}